package main

import (
    "fmt"
    "sort"
    "math"
)

func solution(n int, nums []int) int {
    if n < 3 {
        max := math.MinInt32
        for _, v := range nums {
            if v > max {
                max = v
            }
        }
        return max
    }

    // 去重
    seen := make(map[int]bool)
    var unique []int
    for _, num := range nums {
        if !seen[num] {
            seen[num] = true
            unique = append(unique, num)
        }
    }

    // 降序排序
    sort.Slice(unique, func(i, j int) bool {
        return unique[i] > unique[j]
    })

    if len(unique) < 3 {
        return unique[0]
    }

    return unique[2]
}

func main() {
    fmt.Println(solution(3, []int{3, 2, 1}) == 1)
    fmt.Println(solution(2, []int{1, 2}) == 2)
    fmt.Println(solution(4, []int{2, 2, 3, 1}) == 1)
}